<?php
/**
 * 跃飞科技版权所有 @2017
 * User: 钟贵廷
 * Date: 2017/5/26
 * Time: 16:30
 */
namespace app\admin\controller;
use app\admin\model\AdminMember;
use think\Controller;
use think\Db;
use think\Loader;
use think\Request;

class Manager extends Controller
{
    /**
     * 管理员登录
     */
    public function Login()
    {
        if(Request::instance()->isPost())
        {
            $validate = Loader::validate('AdminMember');
            $adminMember = input('post.');
            if($validate->check($adminMember))
            {
                $adminModel = new AdminMember();
                $adminInfo = $adminModel->where(['username'=>$adminMember['username']])->find();
                if($adminInfo)
                {
                    if($adminInfo['password'] == $adminModel->my_md5($adminMember['password'],$adminInfo['salt'] ) )
                    {
                        session('userId',$adminInfo['id']);
                        session('username',$adminInfo['username']);
                        // 如果用户选择要自动登录那么把用户名和密码保存到COOKIE中N
                        if(isset($adminMember['remember']))
                        {
                            cookie('username', $adminInfo['username'], 7 * 86400);
                            cookie('password', $adminInfo['password'], 7 * 86400);
                        }
                         $this->success('登录成功', url('admin/index/index'));
                    }
                    else
                    {
                        $this->error('用户名或者密码不正确');
                    }
                }
                else
                {
                    $this->error('用户名或者密码不正确');
                }
            }
            else
            {
                $this->error($validate->getError());
            }
        }
        return $this->fetch();
    }

    public function logout()
    {
        session(null);
        cookie('username',null);
        cookie('password',null);
        $this->success('退出啦，下次再来！', url('admin/manager/login'));
    }
}